<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
  <link href="//gmpg.org/xfn/11" rel="profile">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="generator" content="Hugo 0.69.0" />

  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>Django &middot; 宋申奥的博客</title>

  
  <link type="text/css" rel="stylesheet" href="/hugo_blog/css/print.css" media="print">
  <link type="text/css" rel="stylesheet" href="/hugo_blog/css/poole.css">
  <link type="text/css" rel="stylesheet" href="/hugo_blog/css/syntax.css">
  <link type="text/css" rel="stylesheet" href="/hugo_blog/css/hyde.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abril+Fatface|PT+Sans:400,400i,700">


  
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
  <link rel="shortcut icon" href="/favicon.png">

  
  
</head>

  <body class="theme-base-0f ">
  <aside class="sidebar">
  <div class="container sidebar-sticky">
    <div class="sidebar-about">
      <a href="/hugo_blog/"><h1>宋申奥的博客</h1></a>
      <p class="lead">
       这里是宋申奥的个人博客 
      </p>
    </div>

    <nav>
      <ul class="sidebar-nav">
        <li><a href="/hugo_blog/">Home</a> </li>
        
      </ul>
    </nav>

    <p>&copy; 2020. All rights reserved. </p>
  </div>
</aside>

    <main class="content container">
    <div class="post">
  <h1>Django</h1>
  <time datetime=2020-04-21T09:32:11&#43;0800 class="post-date">Tue, Apr 21, 2020</time>
  <h1 id="创建表字段">创建表字段</h1>
<pre><code>from django.db import models


# 导入时间域
from django.utils import timezone

# 基类
class Base(models.Model):

    #创建时间
    create_time = models.DateTimeField(default=timezone.now,null=True)

    class Meta:
        abstract = True

# 用户表
class User(Base):

    #用户名
    username = models.CharField(max_length=200)

    #密码
    password = models.CharField(max_length=200)

    #头像
    img = models.CharField(max_length=200)

    #用户级别,0代表普通用户，1代表超级管理员，2网站编辑
    usertype = models.IntegerField(default=0,null=True)

    #手机号
    phone = models.IntegerField()

    #个人主页
    num = models.IntegerField(default=0,null=True)

    #声明表名
    class Meta:
        db_table = 'user'

</code></pre><h1 id="数据库迁移">数据库迁移</h1>
<p>python manage.py makemigrations 项目名
python manage.py makemigrations myapp
注意：一定要加项目名称，不然myapp中models.py中的表就迁移不到数据库中
然后执行：python manage.py migrate</p>
<h5 id="在数据库迁移不成功时可以选择手动建表用反向迁移将字段迁移到pymodelspy中命令python-managepy-inspectdb--app01modelspy">在数据库迁移不成功时，可以选择手动建表，用反向迁移将字段迁移到pymodels.py中，命令：（python manage.py inspectdb &gt; app01/models.py</h5>
<p>）</p>
<h1 id="导包时注意事项">导包时注意事项</h1>
<p>from 项目名.models import User
from myapp.models import User
在哪个项目中就用哪个项目名</p>
<h1 id="md5加密">md5加密</h1>
<p>要将需要加密的变量进行强转，强转成字符串，然后进行编码</p>
<pre><code># md5加密方法
def make_pasword(mypass):

    #生成md5对象
    md5 = hashlib.md5()

    #转码操作
    mypass_utf8 = str(mypass).encode(encoding='utf-8')

    #进行加密操作
    md5.update(mypass_utf8)

    #返回密文
    return md5.hexdigest()
</code></pre><p><img src="/hugo_blog/0d8a90abc2d78eaa5e01d3fb7f57e6da.jpg" alt="美图"></p>

</div>


    </main>

    
  </body>
</html>
